#!/bin/bash
# ========================================
# RedFire MySQL示例数据初始化脚本
# 版本: v5.0.0 (2025-09-16)
# 功能: 插入测试数据和示例配置
# ========================================

set -e

echo "=========================================="
echo "开始初始化RedFire MySQL示例数据..."
echo "=========================================="

# 检查是否需要插入示例数据
if [ "${INIT_TEST_DATA:-false}" = "true" ]; then
    echo "检测到INIT_TEST_DATA=true，开始插入示例数据..."
else
    echo "跳过示例数据插入 (INIT_TEST_DATA未设置为true)"
    exit 0
fi

# 等待MySQL完全启动
while ! mysqladmin ping -h localhost -u root -p"${MYSQL_ROOT_PASSWORD}" --silent; do
    echo "等待MySQL启动中..."
    sleep 2
done

# 插入示例数据
mysql -u root -p"${MYSQL_ROOT_PASSWORD}" <<EOF

USE redfire_trading;

-- ========================================
-- 插入示例交易账户
-- ========================================

INSERT IGNORE INTO trading_accounts (
    account_id, account_name, account_type, broker_id, currency, 
    balance, available, frozen, position_pnl, close_pnl, status
) VALUES
('DEMO_001', '演示账户001', 'demo', 'DEMO_BROKER', 'CNY', 1000000.00, 950000.00, 50000.00, 5000.00, 2000.00, 'active'),
('DEMO_002', '演示账户002', 'demo', 'DEMO_BROKER', 'CNY', 500000.00, 480000.00, 20000.00, -1000.00, 500.00, 'active'),
('PAPER_001', '模拟账户001', 'paper', 'SIM_BROKER', 'CNY', 2000000.00, 1800000.00, 200000.00, 15000.00, 8000.00, 'active'),
('PAPER_002', '模拟账户002', 'paper', 'SIM_BROKER', 'CNY', 1000000.00, 950000.00, 50000.00, -2000.00, 1000.00, 'active'),
('LIVE_001', '实盘账户001', 'live', 'REAL_BROKER', 'CNY', 100000.00, 95000.00, 5000.00, 500.00, 200.00, 'inactive');

-- ========================================
-- 插入示例持仓数据
-- ========================================

INSERT IGNORE INTO trading_positions (
    account_id, symbol, exchange, direction, volume, frozen, price, pnl, yd_volume
) VALUES
('DEMO_001', '000001.SZ', 'SZSE', 'LONG', 1000, 0, 12.50, 500.00, 800),
('DEMO_001', '000002.SZ', 'SZSE', 'LONG', 2000, 0, 25.80, -200.00, 2000),
('DEMO_001', '600000.SH', 'SSE', 'LONG', 500, 100, 8.90, 150.00, 400),
('DEMO_002', '000001.SZ', 'SZSE', 'LONG', 800, 0, 12.80, -100.00, 800),
('DEMO_002', '600036.SH', 'SSE', 'LONG', 1000, 0, 35.20, 300.00, 1000),
('PAPER_001', '000001.SZ', 'SZSE', 'LONG', 5000, 0, 12.45, 1000.00, 5000),
('PAPER_001', '000002.SZ', 'SZSE', 'LONG', 3000, 500, 25.90, 400.00, 2500),
('PAPER_001', '600000.SH', 'SSE', 'LONG', 2000, 0, 8.85, 200.00, 2000),
('PAPER_001', '600036.SH', 'SSE', 'LONG', 1500, 0, 35.10, 600.00, 1500);

-- ========================================
-- 插入示例策略实例
-- ========================================

INSERT IGNORE INTO strategy_instances (
    strategy_name, instance_id, strategy_class, account_id, vt_symbol, 
    setting, variables, inited, trading, pos
) VALUES
('双均线策略', 'MA_DEMO_001', 'DoubleMaStrategy', 'DEMO_001', '000001.SZ', 
 '{"fast_ma": 5, "slow_ma": 20, "volume": 100, "stop_loss": 0.05}', 
 '{"ma_fast": 12.45, "ma_slow": 12.38, "last_signal": "BUY"}', 
 TRUE, TRUE, 100),

('RSI策略', 'RSI_DEMO_001', 'RsiStrategy', 'DEMO_001', '000002.SZ', 
 '{"rsi_period": 14, "rsi_entry": 30, "rsi_exit": 70, "volume": 200}', 
 '{"rsi_value": 45.6, "position": 0, "last_signal": "HOLD"}', 
 TRUE, FALSE, 0),

('网格策略', 'GRID_PAPER_001', 'GridStrategy', 'PAPER_001', '600000.SH', 
 '{"price_step": 0.1, "volume": 100, "grid_num": 10, "center_price": 8.90}', 
 '{"current_grids": 5, "total_profit": 150.5, "active_orders": 3}', 
 TRUE, TRUE, 500),

('布林带策略', 'BOLL_PAPER_001', 'BollingerStrategy', 'PAPER_001', '600036.SH', 
 '{"period": 20, "std_dev": 2, "volume": 150}', 
 '{"upper_band": 36.2, "lower_band": 34.1, "middle_band": 35.15}', 
 TRUE, FALSE, 0),

('MACD策略', 'MACD_DEMO_002', 'MacdStrategy', 'DEMO_002', '000001.SZ', 
 '{"fast_period": 12, "slow_period": 26, "signal_period": 9, "volume": 100}', 
 '{"macd": 0.15, "signal": 0.12, "histogram": 0.03, "trend": "UP"}', 
 TRUE, TRUE, 200);

-- ========================================
-- 插入示例Tick数据 (最近1小时)
-- ========================================

-- 插入000001.SZ的示例Tick数据
INSERT IGNORE INTO dbtickdata (
    symbol, exchange, datetime, name, volume, turnover, open_interest,
    last_price, last_volume, limit_up, limit_down, open_price, high_price, low_price, pre_close,
    bid_price_1, ask_price_1, bid_volume_1, ask_volume_1, localtime
) VALUES
('000001.SZ', 'SZSE', DATE_SUB(NOW(), INTERVAL 1 HOUR), '平安银行', 12500000, 156250000.00, 0,
 12.50, 1000, 13.75, 11.25, 12.45, 12.58, 12.42, 12.48,
 12.49, 12.51, 500, 300, DATE_SUB(NOW(), INTERVAL 1 HOUR)),

('000001.SZ', 'SZSE', DATE_SUB(NOW(), INTERVAL 50 MINUTE), '平安银行', 12501000, 156262500.00, 0,
 12.51, 1000, 13.75, 11.25, 12.45, 12.58, 12.42, 12.48,
 12.50, 12.52, 400, 600, DATE_SUB(NOW(), INTERVAL 50 MINUTE)),

('000001.SZ', 'SZSE', DATE_SUB(NOW(), INTERVAL 30 MINUTE), '平安银行', 12505000, 156312500.00, 0,
 12.52, 4000, 13.75, 11.25, 12.45, 12.58, 12.42, 12.48,
 12.51, 12.53, 800, 200, DATE_SUB(NOW(), INTERVAL 30 MINUTE));

-- 插入000002.SZ的示例Tick数据
INSERT IGNORE INTO dbtickdata (
    symbol, exchange, datetime, name, volume, turnover, open_interest,
    last_price, last_volume, limit_up, limit_down, open_price, high_price, low_price, pre_close,
    bid_price_1, ask_price_1, bid_volume_1, ask_volume_1, localtime
) VALUES
('000002.SZ', 'SZSE', DATE_SUB(NOW(), INTERVAL 1 HOUR), '万科A', 8500000, 219500000.00, 0,
 25.82, 2000, 28.40, 23.24, 25.75, 25.95, 25.70, 25.78,
 25.81, 25.83, 1000, 800, DATE_SUB(NOW(), INTERVAL 1 HOUR)),

('000002.SZ', 'SZSE', DATE_SUB(NOW(), INTERVAL 45 MINUTE), '万科A', 8502000, 219551600.00, 0,
 25.84, 2000, 28.40, 23.24, 25.75, 25.95, 25.70, 25.78,
 25.83, 25.85, 600, 400, DATE_SUB(NOW(), INTERVAL 45 MINUTE));

-- ========================================
-- 插入示例K线数据 (最近24小时)
-- ========================================

-- 插入1分钟K线数据
INSERT IGNORE INTO dbbardata (
    symbol, exchange, datetime, `interval`, volume, turnover, open_interest,
    open_price, high_price, low_price, close_price
) VALUES
('000001.SZ', 'SZSE', DATE_SUB(NOW(), INTERVAL 60 MINUTE), '1m', 50000, 625000.00, 0, 12.48, 12.52, 12.47, 12.50),
('000001.SZ', 'SZSE', DATE_SUB(NOW(), INTERVAL 59 MINUTE), '1m', 48000, 600000.00, 0, 12.50, 12.53, 12.49, 12.51),
('000001.SZ', 'SZSE', DATE_SUB(NOW(), INTERVAL 58 MINUTE), '1m', 52000, 651000.00, 0, 12.51, 12.54, 12.50, 12.52),

('000002.SZ', 'SZSE', DATE_SUB(NOW(), INTERVAL 60 MINUTE), '1m', 80000, 2064000.00, 0, 25.78, 25.85, 25.76, 25.82),
('000002.SZ', 'SZSE', DATE_SUB(NOW(), INTERVAL 59 MINUTE), '1m', 75000, 1940000.00, 0, 25.82, 25.88, 25.80, 25.84),

('600000.SH', 'SSE', DATE_SUB(NOW(), INTERVAL 60 MINUTE), '1m', 30000, 267000.00, 0, 8.88, 8.92, 8.87, 8.90),
('600036.SH', 'SSE', DATE_SUB(NOW(), INTERVAL 60 MINUTE), '1m', 25000, 877500.00, 0, 35.08, 35.18, 35.05, 35.12);

-- 插入5分钟K线数据
INSERT IGNORE INTO dbbardata (
    symbol, exchange, datetime, `interval`, volume, turnover, open_interest,
    open_price, high_price, low_price, close_price
) VALUES
('000001.SZ', 'SZSE', DATE_SUB(NOW(), INTERVAL 1 HOUR), '5m', 250000, 3125000.00, 0, 12.45, 12.58, 12.42, 12.50),
('000002.SZ', 'SZSE', DATE_SUB(NOW(), INTERVAL 1 HOUR), '5m', 400000, 10320000.00, 0, 25.75, 25.95, 25.70, 25.82),
('600000.SH', 'SSE', DATE_SUB(NOW(), INTERVAL 1 HOUR), '5m', 150000, 1335000.00, 0, 8.85, 8.95, 8.82, 8.90),
('600036.SH', 'SSE', DATE_SUB(NOW(), INTERVAL 1 HOUR), '5m', 120000, 4212000.00, 0, 35.05, 35.20, 35.00, 35.12);

-- ========================================
-- 插入示例订单数据
-- ========================================

INSERT IGNORE INTO dborderdata (
    orderid, symbol, exchange, direction, offset, price, volume, traded, status, datetime, reference
) VALUES
('ORDER_001_001', '000001.SZ', 'SZSE', 'LONG', 'OPEN', 12.50, 100, 100, 'ALLTRADED', DATE_SUB(NOW(), INTERVAL 2 HOUR), 'MA_DEMO_001'),
('ORDER_001_002', '000001.SZ', 'SZSE', 'LONG', 'OPEN', 12.48, 200, 0, 'CANCELLED', DATE_SUB(NOW(), INTERVAL 1 HOUR), 'MA_DEMO_001'),
('ORDER_002_001', '000002.SZ', 'SZSE', 'LONG', 'OPEN', 25.80, 150, 150, 'ALLTRADED', DATE_SUB(NOW(), INTERVAL 3 HOUR), 'RSI_DEMO_001'),
('ORDER_003_001', '600000.SH', 'SSE', 'LONG', 'OPEN', 8.90, 100, 50, 'PARTTRADED', DATE_SUB(NOW(), INTERVAL 30 MINUTE), 'GRID_PAPER_001'),
('ORDER_003_002', '600000.SH', 'SSE', 'SHORT', 'CLOSE', 8.95, 50, 0, 'NOTTRADED', DATE_SUB(NOW(), INTERVAL 15 MINUTE), 'GRID_PAPER_001');

-- ========================================
-- 插入示例成交数据
-- ========================================

INSERT IGNORE INTO dbtradedata (
    tradeid, orderid, symbol, exchange, direction, offset, price, volume, datetime
) VALUES
('TRADE_001_001_001', 'ORDER_001_001', '000001.SZ', 'SZSE', 'LONG', 'OPEN', 12.50, 100, DATE_SUB(NOW(), INTERVAL 2 HOUR)),
('TRADE_002_001_001', 'ORDER_002_001', '000002.SZ', 'SZSE', 'LONG', 'OPEN', 25.80, 150, DATE_SUB(NOW(), INTERVAL 3 HOUR)),
('TRADE_003_001_001', 'ORDER_003_001', '600000.SH', 'SSE', 'LONG', 'OPEN', 8.90, 30, DATE_SUB(NOW(), INTERVAL 35 MINUTE)),
('TRADE_003_001_002', 'ORDER_003_001', '600000.SH', 'SSE', 'LONG', 'OPEN', 8.91, 20, DATE_SUB(NOW(), INTERVAL 32 MINUTE));

-- ========================================
-- 验证示例数据插入
-- ========================================

SELECT '数据插入统计' as info_type;

SELECT 
    'trading_accounts' as table_name,
    COUNT(*) as record_count
FROM trading_accounts
UNION ALL
SELECT 
    'trading_positions' as table_name,
    COUNT(*) as record_count
FROM trading_positions
UNION ALL
SELECT 
    'strategy_instances' as table_name,
    COUNT(*) as record_count
FROM strategy_instances
UNION ALL
SELECT 
    'dbtickdata' as table_name,
    COUNT(*) as record_count
FROM dbtickdata
UNION ALL
SELECT 
    'dbbardata' as table_name,
    COUNT(*) as record_count
FROM dbbardata
UNION ALL
SELECT 
    'dborderdata' as table_name,
    COUNT(*) as record_count
FROM dborderdata
UNION ALL
SELECT 
    'dbtradedata' as table_name,
    COUNT(*) as record_count
FROM dbtradedata
ORDER BY table_name;

EOF

echo "=========================================="
echo "RedFire MySQL示例数据初始化完成！"
echo "插入的数据包括："
echo "  - 5个示例交易账户"
echo "  - 9个示例持仓记录"
echo "  - 5个示例策略实例"
echo "  - 最近1小时的Tick数据"
echo "  - 最近1小时的K线数据"
echo "  - 示例订单和成交数据"
echo "=========================================="
